package com.hotelManagerSystem.controller;


import com.hotelManagerSystem.model.Department;
import com.hotelManagerSystem.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author treasy
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService  iDepartmentService;
    //一:部门列表
    @RequestMapping("/list")
    public String list(Model model){
        List<Department> department_dbList = iDepartmentService.queryDepartmentList();
        model.addAttribute("department_dbList",department_dbList);
        //System.out.println(department_dbList);
        return "admin/index";
    }
    //二:部门添加页面跳转
    @RequestMapping("/addUI")
    public String  addUI(Model model){
        //System.out.println("--------addUI---------");
        return "admin/index";
    }
    //三:部门添加
    @RequestMapping("/add")
    public String add(Model model,Department department){
        //System.out.println("--------add---------");
        boolean b = iDepartmentService.save(department);
        //重新查询一次部门数据库,再跳转界面
        List<Department> department_dbList = iDepartmentService.queryDepartmentList();
        model.addAttribute("department_dbList",department_dbList);
        return "admin/index";
    }
    //四:部门删除
    @RequestMapping("/delete")
    public String delete(Model model,String departmentIdStr){
        System.out.println("--------delete---------");
        //将字符串类型的部门ID转换为Integer类型
        int departmentId = Integer.parseInt(departmentIdStr);
       // System.out.println(departmentId);
        //根据指定的id值来删除数据
        boolean b = iDepartmentService.removeById(departmentId);
        //去查询一次数据库中的数据,再跳转到职工列表界面
        List<Department> department_dbList = iDepartmentService.queryDepartmentList();
        model.addAttribute("department_dbList",department_dbList);
        return "admin/index";

    }
    //五:部门编辑页面跳转
    @RequestMapping("/updateUI")
    public String updateUI(Model model){
        System.out.println("--------updateUI---------");
        return "admin/index";
    }
    //六:部门编辑
    @RequestMapping("/update")
    public String update(Model model,Department department){
       // System.out.println("--------update---------");
       // System.out.println(department);
        boolean b = iDepartmentService.saveOrUpdate(department);
        //去查询一次数据库中的数据,再跳转到职工列表界面
        List<Department> department_dbList = iDepartmentService.queryDepartmentList();
        model.addAttribute("department_dbList",department_dbList);
        return "admin/index";
    }







}

